﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace SimpleUnitity.Log
{
    public class FileLog : ILog
    {
        private readonly string filename;

        public FileLog(string filename)
        {
            this.filename = filename;
        }

        readonly object syncObject = new object();

        public void Write(string message, ELogLevel level)
        {
            lock (syncObject)
            {
                string contents = string.Format("{0:yyyy-MM-dd HH:mm:ss}\t{1}\t{2}{3}", DateTime.Now, level, message, Environment.NewLine);
                try
                {
                    File.AppendAllText(filename, contents);
                }
                catch
                {
                    string path = filename.Substring(0, filename.Replace('\\', '/').LastIndexOf("/"));
                    Directory.CreateDirectory(path);
                    File.AppendAllText(filename, contents);
                }
            }
        }
    }
}
